<?php

class LobbyController extends Controller
{
	const CREATE_GAME = "create";
	const GAME_DETAILS = "details";
	const JOIN_GAME = "join";
	const LEAVE_GAME = "leave";
	const REMOVE_PLAYER = "remove";
	const START_GAME = "start";

	public function DoControll()
	{
		$this->Title( "Game Lobby" );
		$this->JS( "lobby" );
		
		if ( Auth::IsLoggedIn() )
		{
			switch ( $this->view->GetAction() )
			{
				case self::CREATE_GAME :
					try
					{
						require_once 'php/lib/Validation.php';
						$validate = new Validation();

						if ( !$validate->IsValidMaxPlayers( $this->view->GetMaxPlayers() ) )
						{
							throw new Exception( 'Invalid amount of players.' );
						}

						$this->model->CreateGame( Auth::GetUser()->ID(), $this->view->GetMaxPlayers(), $this->view->GetMapID() );
						$this->view->Message( "Game greated." );
					}
					catch ( Exception $e )
					{
						$this->view->Error( $e->getMessage() );
					}

					break;

				case self::JOIN_GAME :
					try
					{
						$this->model->JoinGame( $this->view->GetGameID(), Auth::GetUser()->ID() );
						$this->view->Message( "You joined the game." );
					}
					catch ( Exception $e ) 
					{
						$this->view->Error( $e->getMessage() );
					}

					break;

				case self::LEAVE_GAME :
					try
					{
						$this->model->LeaveGame( $this->view->GetGameID(), Auth::GetUser()->ID() );
						$this->view->Message( "You left the game." );
					}
					catch ( Exception $e )
					{
						$this->view->Error( $e->getMessage() );
					}

					break;

				case self::REMOVE_PLAYER :
					try
					{
						if ( !$this->view->GetPlayer() || strlen( $this->view->GetPlayer() ) < 1 )
						{
							throw new Exception( 'No player has been specified.' );
						}

						if ( $this->view->GetPlayer() == Auth::GetUser()->ID() )
						{
							$this->model->DeleteGame( $this->view->GetGameID(), Auth::GetUser()->ID() );
							$this->view->Message( 'The game has been deleted.' );
						}
						else
						{
							$this->model->RemovePlayer( $this->view->GetGameID(), Auth::GetUser()->ID(), $this->view->GetPlayer() );
							$this->view->Message( 'The player has been removed from the game.' );
						}
					}
					catch ( Exception $e )
					{
						$this->view->Error( $e->getMessage() );
					}

					break;
				
				case self::START_GAME :
					try
					{
						$this->model->StartGame( $this->view->GetGameID(), Auth::GetUser()->ID() );
						$this->view->Message( "The game has been started." );
					}
					catch ( Exception $e )
					{
						$this->view->Error( $e->getMessage() );
					}

					break;
			}
		}


		$games = $this->model->GetGames();

		require_once 'obj/LobbySection.php';
		$lobbySections = array();

		$lobbySections[] = new LobbySection( "lobby", "Lobby", LobbySection::GAMES_TABLE, $games );

		if ( Auth::IsLoggedIn() )
		{
			$lobbySections[] = new LobbySection( "my-games", "My Games", LobbySection::GAMES_TABLE, $this->model->FilterGames( $games, array( 'player' => Auth::GetUser()->ID() ) ) );
			$lobbySections[] = new LobbySection( "create-game", "Create Game", LobbySection::CREATE_GAME, $this->model->GetMaps() );

			if (	$this->view->GetAction() == self::GAME_DETAILS
				||	$this->view->GetAction() == self::REMOVE_PLAYER
				||	$this->view->GetAction() == self::JOIN_GAME
				||	$this->view->GetAction() == self::LEAVE_GAME
				||	$this->view->GetAction() == self::START_GAME )
			{
				$lobbySections[] = new LobbySection( "game-details", "Game Details: " . $this->view->GetGameID(), LobbySection::GAME_DETAILS, $games[$this->view->GetGameID()] );
			}
		}
		
		$this->view->BuildLobby( $lobbySections );

		$this->view->PrintMessages( '<p class="message"><span>Success \o/</span>_MESSAGE_</p>' );
		$this->view->PrintErrors( '<p class="error"><span>Error :(</span>_ERROR_</p>' );
	}
}
